﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Affinity.Flux;
using Affinity.Synthesis;

namespace UserInterface
{
    /// <summary>
    /// Demonstrates an alternate usage of lambda bindings, without the overhead of 
    /// creating an expression tree every time the get accessor is called.
    /// </summary>
    public class DemoAlt2ViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Implementation

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            var propertyChanged = PropertyChanged;
            if (propertyChanged != null) propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        
        #region Time

        [Inlet(Contracts.Time.CurrentTime, InletFlags.ReceiveOnUIThread)]
        public DateTime CurrentTime
        {
            get { return _currentTime; }
            set
            {
                if (value == _currentTime) return;
                _currentTime = value;
                RaisePropertyChanged("CurrentTime");
            }
        }
        private DateTime _currentTime;

        public string CurrentSecondType
        {
            get 
            { 
                // Alternatively, this can be done in the constructor for better performance.
                // Don't need to use the return value, as the binding calls the setter synchronously when we bind.
                CurrentSecondTypeBinding.Bind(this, "CurrentSecondType");
                return _currentSecondType; 
            }
            set 
            {
                if (value == _currentSecondType) return;
                _currentSecondType = value;
                RaisePropertyChanged("CurrentSecondType");
            }
        }
        private string _currentSecondType;

        private static readonly LambdaBinding<string> CurrentSecondTypeBinding = 
            LambdaBinding.Create<DemoAlt2ViewModel, string>(
                _ => _.CurrentTime.Second % 2 == 0 ? "Current second is even" : "Current second is odd");        
        

        #endregion
    }
}
